package de.hs_mannheim.visualscheduler.scheduling;

import java.util.ArrayList;

/**
 * The Class PriorityBased implements priority-based scheduling.
 */
public class PriorityBased extends AbstractScheduler {
	private final boolean preemptive;
	
	/**
	 * Instantiates a new priority based scheduler.
	 * 
	 * @param preemptive
	 *            the preemptive
	 */
	public PriorityBased(boolean preemptive) {
		this.preemptive = preemptive;
	}

	@Override
	protected String getAlgorithmName() {
		return (preemptive ? "Premptive-" : "") + "Priority-Based " + "(" + (preemptive ? "P" : "")  + "PB)";
	}

	@Override
	protected ScheduleResult performSchedulingBlock(ArrayList<Process> active) {
		int highestPriority = 0;
		Process best = null;
		for(Process current: active) {
			int currentPriority = current.definition.getPriority(); 
			
			if(currentPriority > highestPriority) {
				highestPriority = currentPriority;
				best = current;
			}
		}
		return new ScheduleResult(best, best.getTimeleft());
	}

	@Override
	protected void prepareForScheduling(int timeSliceLength) {
	}

	@Override
	protected boolean isPreemptive() {
		return preemptive;
	}
}
